Ext.define('${app.appName}.view.${app.lowerName}.${app.className}Grid',{
    extend:'Ext.grid.Panel',
    alias:'widget.${app.className}Grid',
    multiSelect:true,
    enableKeyNav:true,
    border:false,
    columnLines:true,
    stripeRows: true,
    loadMask: true,
    lock:true,
    oldId:'${app.className}_',
    columns:[<#list app.items as item>{
        header:'${item.name}',
        dataIndex:'${item.name}',
        flex:2,
        field:{}
    }<#if item_has_next>,</#if></#list>],
    selModel:{
        selType:'checkboxmodel'
    },
    viewConfig : {
        forceFit : true,
        scrollOffset : 0
    },
    initComponent:function(){
        this.tbar=[{
            text:'Add ${app.className}',
            iconCls:'user-add',
            scope: this,
            handler: this.onAddClick
        },'-',{
            text:'Update ${app.className}',
            iconCls:'user-edit',
            itemId:this.oldId+"Update",
            scope: this,
            disabled: this.lock,
            handler:this.onEditorClick
        },'-',{
            text:'Remove ${app.className}',
            disabled: this.lock,
            itemId:this.oldId+"Delete",
            iconCls:'user-delete',
            scope:this,
            handler:this.onDeleteClick
        },'->',{
            text:'refresh',
            iconCls:'refresh-icon',
            handler:function(){
                console.log('refresh','click');
            },
            scope: this
        },'-',{
            text:'export',
            iconCls:'print-icon',
            handler:function(){
                console.log('export','click');
            },
            scope: this
        },'-',{
            text:'help',
            iconCls:'help-ico',
            handler:function(){
                console.log('help','click');
            },
            scope: this
        }];
        this.store=Ext.create('${app.appName}.store.${app.className}s');
        this.dockedItems=[{
            xtype:'pagingtoolbar',
            store:this.store,
            dock:'bottom',
            displayInfo:true,
            plugins: Ext.create('Ext.ux.ProgressBarPager')
        }];
        this.editing = Ext.create('Ext.grid.plugin.CellEditing');
        this.plugins = [this.editing];
        this.callParent(arguments);
    },
    listeners: {
        selectionchange: function(selModel, selected) {
            this.down('#'+this.oldId+"Update").setDisabled(selected.length === 0);
            this.down('#'+this.oldId+"Delete").setDisabled(selected.length === 0);
        },
        edit:function(editor, e, options){
            var record = e.record.raw;
            var data = e.record.data;
            var val = e.value;
            if(e.originalValue!=e.value){
                console.warn('id:'+record.id,'attr:'+e.field +"  value: "+e.value);
                Ext.Ajax.request({
                    url: '/action/${app.lowerName}/updateAttr.action',
                    params: {
                        id: record.id,
                        attr:e.field,
                        val:val
                    },
                    success: function(response){
                        var text = eval('('+response.responseText+')');//Ext.JSON.decode(response.responseText).message;
                        console.log('success: ',text.flag);
                    }
                });
            }
            
            e.record.commit();
        }
    },
    onSaveRecord:function(form, data){
        this.store.insert(0,data);
    },
    onUpdateRecord:function(form, data){
        var selection = this.getView().getSelectionModel().getSelection()[0];
        Ext.apply(selection.data,data);
        selection.commit();
    },
    onDeleteClick: function(){
        var selections = this.getView().getSelectionModel().getSelection();
        var ids ="";
        for(var i=0;i<selections.length;i++){
            ids+=selections[i].data.id;
            if(i<selections.length-1){
                ids+=',';
            }
        }
        var currentView = this;
        if (selections) {
            Ext.Msg.confirm("系统提示", "确定要删除选中的记录吗?", function (_btn) {
                if ('yes' == _btn) {
                    Ext.Ajax.request({
                        url: basePath+'/action/${app.lowerName}/deletes.action',
                        params: {
                            ids: ids
                        },
                        success: function(response){
                            var text = eval('('+response.responseText+')');//Ext.JSON.decode(response.responseText).message;
                            currentView.store.remove(selections);
                            
                            var tc = currentView.store.totalCount - selections.length;
                            var pageSize = currentView.store.pageSize;
                            var cp = currentView.store.currentPage;
                            var num = (tc + pageSize - 1) / pageSize;
                            var pageNum = cp>num ? num:cp;
                            currentView.store.loadPage(pageNum); 
                        }
                    });
                }
            },this);
        }else{
            Ext.MessageBox.alert("提示","请选择数据行!");
        }
    },
    onEditorClick: function(){
        var selection = this.getView().getSelectionModel().getSelection()[0];
        if (selection){
            this.editorWindow = Ext.create("gwtjs.window.${app.className}Window",{
                title:"${app.className}管理@修改"
            });
            this.editorWindow.setActiveRecord(selection);
            this.editorWindow.on('create',this.onUpdateRecord,this);
            this.editorWindow.show();
        }else{
            Ext.MessageBox.alert("提示","请选择数据行!");
        }
    },
    onAddClick: function(){
        this.editorWindow = Ext.create("gwtjs.window.${app.className}Window",{
            title:"${app.className}管理@新增"
        });
        this.editorWindow.on('create',this.onSaveRecord,this);
        this.editorWindow.show();
    }
});